// 引入express
const express = require("express");
//1.引入body-parser 解析post和put提交方式的数据,在上面统一引入的地方引入
const bodyParser = require("body-parser");
// 引入用户的路由模块c
const user = require("./routes/users.js");
// 引入employee路由模块
const employee = require("./routes/employee.js");
//引入 部门路由模块
const department = require("./routes/department.js");
//创建express对象
const app = express();
//设置cors
app.use("*", function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    if (req.method == "OPTIONS") res.send(200); /*让options请求快速返回*/
    else next();
})

// 配置body-parser，在跨域之后配置
// 2.配置form表单提交数据的请求体解析器
app.use(bodyParser.urlencoded({
    extended: false
}))
//3.配置json提交数据的请求体解析器
app.use(bodyParser.json());

// 引入路由
app.use("/users", user);
// 引入员工路由
app.use("/employee", employee);
// 引入部门的路由
app.use("/department", department);
//启动服务监听
app.listen(3000, () => {
    console.log("服务已启动")
})